#!/usr/env python

import csv
from datetime import datetime

def write_doc_comment(test_file, author, date, case_id, purpose, priority):
    test_file.write("/**\n")
    test_file.write(" * @author: %s\n" % author)
    test_file.write(" * @date: %s\n" % date.isoformat())
    test_file.write(" * @version: 0.1\n")
    test_file.write(" * @TCID: %s\n" % case_id)
    test_file.write(" * Purpose: test %s\n" % purpose)
    test_file.write(" * Priority: %s\n" % priority)
    test_file.write(" */\n")

def write_test_method(test_file, test_name, sideA, sideB, sideC, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Triangle t = new Triangle(%s, %s, %s);\n" % (sideA, sideB, sideC))
    if expected == "true":
        test_file.write("    assertTrue(Triangle.isTriangle(t));\n")
    else:
        test_file.write("    assertFalse(Triangle.isTriangle(t));\n")
    test_file.write("}\n\n")

def write_test_method2(test_file, test_name, sideA, sideB, sideC, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Triangle t = new Triangle(%s, %s, %s);\n" % (sideA, sideB, sideC))
    test_file.write("    assertEquals(\"%s\", Triangle.getType(t));\n" % expected)
    test_file.write("}\n\n")

with open('091250198_unit_test_design.csv') as csv_file:
    reader = csv.reader(csv_file)
    reader.next() # skip title line
    
    header = reader.next()
    case_idx = header.index('Test Case')
    name_idx = header.index('Name')
    sda_idx = header.index('SideA')
    sdb_idx = header.index('SideB')
    sdc_idx = header.index('SideC')
    exp_idx = header.index('Expected')
    pps_idx = header.index('Purpose')
    prt_idx = header.index('Priority')
    
    # print name_idx, sda_idx, sdb_idx, sdc_idx, exp_idx, pps_idx, prt_idx

    today = datetime.today()
    with open('methods.java', 'w') as test_file:
        for row in reader:
            test_case = row[case_idx]
            test_name = row[name_idx]
            sideA = row[sda_idx]
            sideB = row[sdb_idx]
            sideC = row[sdc_idx]
            expected = row[exp_idx]
            purpose = row[pps_idx]
            priority = row[prt_idx]

            write_doc_comment(test_file, "kavinyao", today, test_case, purpose, priority)
            if "isTriangleTest" in test_name:
                write_test_method(test_file, test_name, sideA, sideB, sideC, expected)
            else: # getTypeTest
                write_test_method2(test_file, test_name, sideA, sideB, sideC, expected)
